package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.util.HashMap;
import java.util.Map;

import com.iwhalecloud.bss.pub.client.constanst.Symbols;
import org.springframework.beans.factory.annotation.Autowired;

import com.iwhalecloud.bss.kite.common.cache.impl.KiteContextCache;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalQuerySeqHelper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;

/**
* @Description: 资费计划标识
*
* @author lyx
* @date 2020年12月21日
* @version V1.0
* @Package com.iwhalecloud.bss.kite.cucc.service.attr.handler
*/
public class UniversalLinkFeePlanIdAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void def(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteAttr accAttr = querySceneInstService.getCommonAttrsByFieldName(data.getSceneInstId(), "business_number", "113");
        String name = accAttr.getValue();
        CuccContextCustDTO custDTO = KiteContextCache.get(KiteStringUtils.join(ContextUtil.getHttpSessoinId(), Symbols.HASH, data.getCustId(), Symbols.AT, "custInfo"));
        // 序列
        Map<String, String> inParam = new HashMap<>();
        inParam.put("SEQ_TYPE", "WLW_RATEPLAN_ID");
        inParam.put("SYS_TYPE", "1");
        inParam.put("SYS_SOURCE", "");
        String seq = LocalQuerySeqHelper.getSeqBySeqType(inParam, data.getSceneInstId(), custDTO);
        name = name + "_ZF_" + seq;
        // 200003698 资费计划名称
        if (KiteStringUtils.equals("200003698", attr.getFieldName())) {
            name = "待生成";
        }
        attr.setValue(name);
        attr.setValueDesc(name);
        attr.setDefaultValue(name);
        super.set(attr, inst, name, data);
    }

    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        //模版还原时，需要生成新的标识
        if ("sceneTempl".equals(data.getExtAttributes().get("entry"))) {
            this.def(attr, inst, data);
        }
    }
}
